package com.hkfn.rfb.rfb.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.hkfn.rfb.MainActivity;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.bean.BannerModel;
import com.hkfn.rfb.common.bean.ConstData;
import com.hkfn.rfb.common.fragment.BaseFragment;
import com.hkfn.rfb.common.utils.DialogUtils;
import com.hkfn.rfb.databinding.FRfbSystemOperationBinding;
import com.hkfn.rfb.rfb.adapter.RfbSystemOperationAdapter;
import com.hkfn.rfb.rfb.bean.RfbSystemOperationModel;
import com.xh.common.thread.AsyncExecutor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 系统运维页面
 */
public class RfbSystemOperationFragment extends BaseFragment {

    private BaseActivity activity;
    private Bundle bundle;
    private FRfbSystemOperationBinding fRfbSystemOperationBinding;

    private RfbSystemOperationAdapter rfbSystemOperationAdapter;

    private Dialog datePickerDialog;
    private String currentDateStr;

    private Dialog chooseOrderTypeDialog;
    private Dialog chooseOrderStatusDialog;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activity = getMyActivity();
        bundle = getArguments();
        fRfbSystemOperationBinding = FRfbSystemOperationBinding.inflate(inflater, container, false);
        initView();
        setListData();
        return fRfbSystemOperationBinding.getRoot();
    }

    private void initView() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYY_MM_DD);
        fRfbSystemOperationBinding.tvSelectDate.setText(simpleDateFormat.format(new Date()));
        currentDateStr = simpleDateFormat.format(new Date());
        fRfbSystemOperationBinding.tvSelectDate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String dateStr = fRfbSystemOperationBinding.tvSelectDate.getText().toString();
                if (!dateStr.equals(currentDateStr)){
                    currentDateStr = dateStr;
                    setListData();
                }

            }
        });
        fRfbSystemOperationBinding.llSelectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.MONTH, -1);
                DialogUtils.closeDialog(datePickerDialog);
                datePickerDialog = DialogUtils.createDatePickerDialog(activity, fRfbSystemOperationBinding.tvSelectDate,
                        calendar.getTimeInMillis(), System.currentTimeMillis());
                datePickerDialog.show();
            }
        });

        fRfbSystemOperationBinding.tvOrderType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseOrderTypeDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("工单类型1");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("工单类型2");
                bannerModels.add(bannerModel1);
                BannerModel bannerModel2 = new BannerModel();
                bannerModel2.setTitle("工单类型3");
                bannerModels.add(bannerModel2);
                chooseOrderTypeDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择工单类型", -1, (v12, position, data) -> {
                    fRfbSystemOperationBinding.tvOrderType.setText(((BannerModel)data).getTitle());
                    setListData();
                    DialogUtils.closeDialog(chooseOrderTypeDialog);
                });
                DialogUtils.showDialog(chooseOrderTypeDialog);
            }
        });

        fRfbSystemOperationBinding.tvOrderStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtils.closeDialog(chooseOrderStatusDialog);
                List<BannerModel> bannerModels = new ArrayList<>();
                BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("未完成");
                bannerModels.add(bannerModel);
                BannerModel bannerModel1 = new BannerModel();
                bannerModel1.setTitle("已完成");
                bannerModels.add(bannerModel1);
                chooseOrderStatusDialog = DialogUtils.createChooseListDialog(activity, bannerModels, "选择工单状态", -1, (v1, position, data) -> {
                    fRfbSystemOperationBinding.tvOrderStatus.setText(((BannerModel)data).getTitle());
                    setListData();
                    DialogUtils.closeDialog(chooseOrderStatusDialog);
                });
                DialogUtils.showDialog(chooseOrderStatusDialog);
            }
        });


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fRfbSystemOperationBinding.rlvRfbFormData.setLayoutManager(linearLayoutManager);
        rfbSystemOperationAdapter = new RfbSystemOperationAdapter(activity);
        fRfbSystemOperationBinding.rlvRfbFormData.setAdapter(rfbSystemOperationAdapter);
    }

    private void setListData(){
        rfbSystemOperationAdapter.clear();
        AsyncExecutor.execute(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYY_MM_DD);
                Date chooseDate = null;
                try {
                    chooseDate = simpleDateFormat.parse(currentDateStr);
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                if (null != chooseDate){
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(chooseDate);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    long startTime = calendar.getTimeInMillis();
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                    long endTime = calendar.getTimeInMillis();
                    String orderType = null;
                    switch (fRfbSystemOperationBinding.tvOrderType.toString()){
                        case "充放电故障":
                            orderType = "0";
                            break;
                        case "消防故障":
                            orderType = "1";
                            break;
                        case "加热故障":
                            orderType = "2";
                            break;
                    }
                    String orderStatus = null;
                    switch (fRfbSystemOperationBinding.tvOrderStatus.toString()){
                        case "未完成":
                            orderStatus = "0";
                            break;
                        case "已完成":
                            orderStatus = "1";
                            break;
                    }
                    List<RfbSystemOperationModel> list = new ArrayList<>();
                    //List<RfbOrderModel> list = ((MainActivity) activity).queryEmsOrderList(startTime, endTime, orderType, orderStatus);
                    for (int i = 0; i < 15; i++) {
                        RfbSystemOperationModel rfbSystemOperationModel = new RfbSystemOperationModel();
                        rfbSystemOperationModel.set_id(i);
                        rfbSystemOperationModel.setOrderType("0");
                        rfbSystemOperationModel.setStartTime(startTime);
                        rfbSystemOperationModel.setStartPerson("0");
                        rfbSystemOperationModel.setDeviceInfo("PCS");
                        rfbSystemOperationModel.setOrderStatus("0");
                        rfbSystemOperationModel.setAlarmStatus("0");
                        rfbSystemOperationModel.setOrderDescription("工单描述工单描述");
                        rfbSystemOperationModel.setCompleteTime(1712048450000l);
                        list.add(rfbSystemOperationModel);
                    }
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (null != list && list.size() > 0){
                                rfbSystemOperationAdapter.addDatas(list);
                                int completeNum = 0;
                                for (RfbSystemOperationModel rfbSystemOperationModel : list) {
                                    if ("1".equals(rfbSystemOperationModel.getOrderStatus())){
                                        // 已完成
                                        completeNum++;
                                    }
                                }
                                fRfbSystemOperationBinding.tvOrderCompleteNum.setText("" + completeNum);
                                fRfbSystemOperationBinding.tvOrderUncompleteNum.setText("" + (list.size() - completeNum));
                            }else {
                                fRfbSystemOperationBinding.tvOrderCompleteNum.setText("0");
                                fRfbSystemOperationBinding.tvOrderUncompleteNum.setText("0");
                            }
                        }
                    });

                }
            }
        });
    }

    @Override

    public void onResume() {
        super.onResume();
        updateViewData();
        ((MainActivity) activity).setMenuTitle(ConstData.EMS_MAIN);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        fRfbSystemOperationBinding = null;
        DialogUtils.closeDialog(datePickerDialog);
        DialogUtils.closeDialog(chooseOrderTypeDialog);
        DialogUtils.closeDialog(chooseOrderStatusDialog);
    }

    @Override
    public void updateViewData() {
        super.updateViewData();

    }
}
